<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/2/2 0002
 * Time: 11:06
 */

namespace app\admin\model;


use think\Model;

class Member extends Model
{
    protected $autoWriteTimestamp = "datetime";
    protected $dateFormat = 'Y-m-d H:i:s';

    protected $createTime = "date_entered";
    protected $updateTime = "update_entered";

    protected static function init()
    {
        self::beforeInsert(function ($member) {

        });
    }

    /**
     * 登录
     * @param $data
     * @return array|bool
     */
    public function login($data)
    {
        $member = $this->where('username', '=', $data['username'])->findOrEmpty()->toArray();
        if (!$member) {
            return false;
        }
        if (createPassword($data['password'], $member['salt'])!= $member['password']) {
            return false;
        }
        return $member;
    }

    public function getList($offset = 0, $limit=10, $where)
    {
        return $this->where($where)->limit($offset, $limit)->order('id', 'desc')->select()->toArray();
    }

    public function getListCount($where)
    {
        return $this->where($where)->count();
    }

    public function add($data)
    {
        return $this->insert($data);
    }

    public function edit($id, $data)
    {
        return $this->where('id', $id)->update($data);
    }

    public function getOneById($id)
    {
        return $this->where("id", $id)->findOrEmpty()->toArray();
    }

    public function del($id)
    {
        return $this->where("id", $id)->delete();
    }

    public function groups()
    {
        return $this->belongsToMany("AuthGroup", "auth_group_access", "group_id", "uid");
    }
}